package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * @author Sanmier
 * @create 2020-12-01 10:18
 */
@Configuration
public class CorsConfig {
    //制作跨域
    @Bean
    public CorsWebFilter corsWebFilter(){
        //cors跨域配置对象
        CorsConfiguration configuration = new CorsConfiguration();
        //设置跨域的相关参数
        configuration.addAllowedMethod("*");
        configuration.addAllowedHeader("*");
        configuration.addAllowedOrigin("*");
        configuration.setAllowCredentials(true);
        //返回当前对像
        UrlBasedCorsConfigurationSource configurationSource = new UrlBasedCorsConfigurationSource();
        // 配置哪些需要过滤的路径
        configurationSource.registerCorsConfiguration("/**",configuration);
        // CorsConfigurationSource  是个接口
        return new CorsWebFilter(configurationSource);
    }
}
